﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using UI.Online;
using UI.Online.SubForms;
using System.IO;

namespace UI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmOnlineUI2());


            // Disable checking serial key for developing purpose - Anh
            /*
            if (ValidateSoftware())
            {
                DateTime InstalledTime = Properties.Settings.Default.InstalledDateTime;
                TimeSpan ts = DateTime.Now.Subtract(InstalledTime);
                if (ts.TotalSeconds < 15552000)
                //if (ts.TotalSeconds < 3600)
                {
                    Application.Run(new frmOnlineUI2());
                }
                else
                {
                    MessageBox.Show(
                            "Your software has expired.\nPlease contact CNA Software to purchase new key.",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                    Application.Run(new frmRegister());
                }
            }
            else
            {
                MessageBox.Show(
                        "Your software is not licienced.\nPlease contact CNA Software to purchase new key.",
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
                Application.Run(new frmRegister());
            }
            */
        }

        static string GenerateHashCode(string privateKey, string serialKey)
        {
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            byte[] preHash = System.Text.Encoding.UTF32.GetBytes(privateKey + serialKey);
            byte[] hash = sha.ComputeHash(preHash);
            return System.Convert.ToBase64String(hash, 0, 15);
        }

        static bool ValidateSoftware()
        {
            string PrivateKey;
            string SerialKey;

            string KeyFile = @"C:\Temp\Key.txt";
            string PrivateKeyFile = @"C:\Temp\PrivateKey.txt";
            string HashCodeFile = @"C:\Temp\HashCodes.txt";

            try
            {
                using (StreamReader s = new StreamReader(PrivateKeyFile))
                {
                    PrivateKey = s.ReadLine();
                }
                using (StreamReader s = new StreamReader(KeyFile))
                {
                    SerialKey = s.ReadLine();
                }
                string hashCode = GenerateHashCode(PrivateKey, SerialKey);
                using (StreamReader sr = new StreamReader(HashCodeFile))
                {
                    string allHashCodes = sr.ReadToEnd();
                    if (allHashCodes.Contains(hashCode))
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }
    }
}
